package com.ontheroad.mysql.sport.service;

import cn.modoumama.page.PageObject;
import cn.modoumama.service.base.impl.BaseServiceImpl;
import com.ontheroad.api.request.Request;
import com.ontheroad.api.request.SprotRequest;
import com.ontheroad.api.response.Response;
import com.ontheroad.api.response.SportResponse;
import com.ontheroad.mysql.path.mapper.PathUserMapper;
import com.ontheroad.mysql.sport.mapper.SportMapper;
import com.ontheroad.path.model.PathUserModel;
import com.ontheroad.sport.model.SportModel;
import com.ontheroad.sport.service.SportService;
import com.ontheroad.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by kedong on 2017/6/27 0027.
 */
@Service
@Transactional
public class SportServiceImpl extends BaseServiceImpl<SportModel, Integer> implements SportService {

    Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private SportMapper mapper;

    @Autowired
    private PathUserMapper pathUserMapper;

    @Autowired
    public void setMapper(SportMapper mapper) {
        setGenericMapper(mapper);
    }

    @Override
    public Response uploadSport(SprotRequest request) throws Exception {
        SportModel sportModel = new SportModel();
        sportModel.setUserid(request.getUserId().intValue());
        sportModel.setDuration(request.getDuration());
        Date endTime = DateUtil.stringTodate(request.getEndTime(), "yyyy-MM-dd HH:mm");
        sportModel.setEndTime(endTime);
        sportModel.setKilometre(request.getKilometre());
        sportModel.setType(request.getType());
        mapper.insertModel(sportModel);
        return Response.SUCCESS();
    }

    @Override
    public Response getSportList(Request request) throws Exception {
        Long userId = request.getUserId();

        PageObject pageObject = new PageObject();
        pageObject.addCondition("userid", userId);
        pageObject.setCurrPage(request.getPageNum());
        pageObject.setPageSize(request.getPageSize());
        List<SportModel> list = findModelsByCondition(pageObject);
        Map<String, Object> map = new HashMap<>();
        for (SportModel sportModel : list) {
            map.put("sportid", sportModel.getId());
            List<PathUserModel> userModels = pathUserMapper.findModelsByCondition(map);
            if (userModels != null && userModels.size() > 0) {
                PathUserModel userModel = userModels.get(0);
                if (userModel != null) {
                    sportModel.setPathId(userModel.getId().intValue());
                }
            }
        }
        return new SportResponse(list);
    }

    @Override
    public Response deleteSportList(SprotRequest request) throws Exception {
        Integer sportId = request.getSportId();
        // 删除运动数据
        mapper.deleteById(sportId);
        // 更新轨迹中的运动ID为空
        pathUserMapper.updateSportId(sportId);
        return Response.SUCCESS();
    }
}
